<!DOCTYPE html>
<html class="x-admin-sm" xmlns:th="http://www.thymeleaf.org">
<head>
    <title>管理后台-定时任务列表</title>
    <header th:replace="header::html"/>
</head>
<body>
    <div class="x-nav">
        <span class="layui-breadcrumb">
            <a href="#">首页</a>
            <a href="#">任务管理</a>
            <a><cite>任务列表</cite></a>
        </span>
        <a class="layui-btn layui-btn-small refresh" onclick="location.reload()" title="刷新">
            <i class="layui-icon layui-icon-refresh"></i>
        </a>
    </div>
    <div class="layui-fluid">
        <div class="layui-row layui-col-space15">
            <div class="layui-col-md12">
                <div class="layui-card">
                    <div class="layui-card-body" permission="sys:task:query">
                        <form class="layui-form layui-col-space5">
                            <div class="layui-inline layui-show-xs-block">
                                <input type="text" name="jobName" placeholder="请输入任务名" class="layui-input">
                            </div>
                            <div class="layui-inline layui-show-xs-block">
                                <button class="layui-btn" lay-submit="" lay-filter="search">
                                    <i class="layui-icon layui-icon-search"></i>
                                </button>
                            </div>
                        </form>
                    </div>
                    <div class="layui-card-header">
                        <button class="layui-btn" onclick="xadmin.open('新增任务','/task/add', null, '65%')" permission="sys:task:add">
                            <i class="layui-icon layui-icon-add-circle-fine"></i>添加
                        </button>
                        <button class="layui-btn layui-btn-danger" onclick="batchDelete()" permission="sys:task:del">
                            <i class="layui-icon layui-icon-delete"></i>批量删除
                        </button>
                    </div>
                    <div class="layui-card-body">
                        <table id="taskTable" lay-filter="taskTable"></table>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script type="text/html" id="toolBar">
        <a class="layui-btn layui-btn-xs" lay-event="run" permission="sys:task:edit"><i class="layui-icon">&#xe652;</i>执行</a>
        <a class="layui-btn layui-btn-xs" lay-event="edit" permission="sys:task:edit"><i class="layui-icon">&#xe642;</i>编辑</a>
        <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del" permission="sys:task:del"><i class="layui-icon">&#xe640;</i>删除</a>
    </script>
    <script type="text/html" id="jobStatusTpl">
        <input type="checkbox" name="jobStatus" value="{{d.id}}" lay-skin="switch" lay-text="运行|停止" lay-filter="jobStatus" {{ d.jobStatus? 'checked' : '' }}>
    </script>
    <script type="text/javascript">
        var table;

        layui.use(['table', 'form', 'layer'], function () {
            table = layui.table;
            var form = layui.form,
                layer = layui.layer;

            // 表格渲染
            renderTable(new FormData());

            // 监听工具条
            table.on('tool(taskTable)', function (obj) {
                var data = obj.data;

                // 执行任务
                if (obj.event === 'run') {
                    var loadIndex = layer.load(2);
                    $.post("/task/run", {id: data.id}, function (res) {
                        layer.close(loadIndex);
                        if (res.httpCode != 200 || res.retCode != 0) {
                            layer.alert(res.retMsg);
                        } else {
                            layer.alert('任务执行成功！', {icon: 1});
                        }
                    });
                } else if (obj.event === 'edit') {
                    // 编辑操作
                    xadmin.open('编辑任务信息', '/task/edit?id=' + data.id, null, '65%');
                } else if (obj.event === 'del') { // 删除操作
                    layer.confirm('确认要删除吗？', function (index) {
                        // 发送请求异步删除数据
                        $.ajax({
                            url: '/task/delete',
                            type: "DELETE",
                            data: {id: data.id},
                            dataType: 'json',
                            success: function (res) {
                                if (res.httpCode != 200 || res.retCode != 0) {
                                    layer.alert(res.retMsg);
                                } else {
                                    layer.alert("删除成功", {icon: 1});
                                    // 刷新表格
                                    table.reload('taskTable');
                                }
                            }
                        });
                        //关闭弹窗
                        layer.close(index);
                    });
                }
            });

            // 搜索功能
            form.on('submit(search)', function (data) {
                // 表格渲染
                renderTable(data.field)
                return false;
            });

            // 监听任务状态切换
            form.on('switch(jobStatus)', function (obj) {
                var loadIndex = layer.load(2);
                $.post("/task/changeStatus", {id: this.value, jobStatus: obj.elem.checked}, function (res) {
                    layer.close(loadIndex);
                    if (res.httpCode != 200 || res.retCode != 0) {
                        layer.alert(res.retMsg);
                    } else {
                        layer.tips('任务' + ((obj.elem.checked) ? "运行" : "停止"), obj.othis, {tips: 3});
                    }
                });
            });
        });

        /**
         * 表格渲染
         * @param field
         */
        function renderTable(field) {
            // 表格渲染
            table.render({
                elem: '#taskTable',
                url: '/task/list', //数据接口
                page: true, //开启分页
                where: {//查询条件
                    "jobName": field.jobName
                },
                parseData: function (res) { //res 即为原始返回的数据
                    return {
                        "code": res.retCode,     //解析接口状态
                        "msg": res.retMsg,       //解析提示文本
                        "count": res.data.total, //解析数据长度
                        "data": res.data.datas   //解析数据列表
                    };
                },
                cols: [
                    [   //表头
                        {type: 'checkbox', fixed: 'left'},
                        {field: 'id', title: 'ID', sort: true},
                        {field: 'jobName', title: '任务名称', sort: true},
                        {field: 'description', title: '任务描述'},
                        {field: 'jobGroup', title: '任务分组'},
                        {field: 'beanClass', title: '任务类'},
                        {field: 'cronExpression', title: 'cron表达式'},
                        {field: 'jobStatus', title: '任务状态', toolbar: '#jobStatusTpl'},
                        {title: '操作', toolbar: '#toolBar', fixed: 'right', width: 240}
                    ]
                ],
                limits: [10, 15, 20, 25, 50, 100],
                limit: 10,
                done:function () {
                    checkPermissoin();
                }
            });
        }

        /**
         * 批量删除
         */
        function batchDelete() {
            var checkStatus = table.checkStatus('taskTable');
            var data = checkStatus.data;
            if (data.length == 0) {
                layer.msg("请选择要删除的数据！");
                return;
            }
            layer.confirm('确认要删除选中的【' + data.length + '】条数据吗？', function (index) {
                var ids = new Array();
                // 遍历所有选择的行数据，取每条数据对应的ID
                for (var i = 0; i < data.length; i++) {
                    ids[i] = data[i].id;
                }

                // 发送请求异步删除所有被选中的数据
                $.ajax({
                    url: '/task/batchDelete',
                    type: "DELETE",
                    data: {ids: ids},
                    dataType: 'json',
                    success: function (res) {
                        if (res.httpCode != 200 || res.retCode != 0) {
                            layer.alert(res.retMsg);
                        } else {
                            layer.alert("删除成功", {icon: 1});
                            // 刷新表格
                            table.reload('taskTable');
                        }
                    }
                });
                layer.close(index);
            });
        }
    </script>
</body>
</html>